<?php

error_reporting(0); //This would ensure that all the errors are not displayed on screen and are instead directed to log4php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Category extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
        $this->load->model('category/category_model');
    }

    public function index() {
    	$this->config->load('site', TRUE);
        $layout_categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $layout_categorys;
        $this->_data['status'] = $this->config->item('status', 'site');
        
        $this->load->admintemplate('list', $this->_data);
    }

    public function add() {
        $this->load->library('form_validation');
        $this->config->load('site', TRUE);
        $this->config->load('form_validate', TRUE);
        $this->form_validation->set_rules($this->config->item('category', 'form_validate'));
        $this->_data['status'] = $this->config->item('status', 'site');
        
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('add', $this->_data);
        } else {
        	if ($this->input->post("category_name")) {
                $params['category_name'] = $this->input->post('category_name');
                $params['content_letter'] = $this->input->post('content_letter',TRUE);
                $params['status'] = $this->input->post('status');
                $this->category_model->insert($params);
                redirect('category');
            }
        }
    }

    public function edit($id) {
        $this->load->library('form_validation');
        $this->config->load('site', TRUE);
        $this->config->load('form_validate', TRUE);
        $this->form_validation->set_rules($this->config->item('category', 'form_validate'));
        $this->_data['status'] = $this->config->item('status', 'site');
        $category = $this->category_model->find_id($id);
        $this->_data['category'] = $category;
        
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('edit', $this->_data);
            
        } else {
            $params['category_name'] = $this->input->post('category_name',TRUE);
            $params['content_letter'] = $this->input->post('content_letter',TRUE);
            $params['status']	 = $this->input->post('status');
            $this->category_model->update($id,$params);
            redirect('category');
        }
    }

    public function delete() {
        $id = $this->input->post('id');
        if ($this->category_model->delete($id) ) {
            echo 1;
        } else {
            echo 0;
        }
        die;
    }

    //status of layout_category
    function changestatus() {
        $id = $this->input->post('id');
        $row = $this->category_model->getRow($id);
        $status = ( $row->status == 0 ) ? 1 : 0;
        $this->category_model->updateRow($id, array('status' => $status));
        echo $status;
        die;
    }

    public function user_check($str) {
        if ($str == 'test') {
            $this->form_validation->set_message('user_check', 'The %s field can not be the word "test"');
            return FALSE;
        } else {
            $this->form_validation->set_message('user_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
    }

}
